import { defineStore } from 'pinia'
// 如何设计这个东西 1 初始化数据 2 清空数据 3 需要什么数据 4 下一页 5 提交数据重新排序 6 预览数据
export const quoteStore = defineStore('quoteStore', {
  state: () => {
    return {
      hazardCites: [
        {
          distance: [
            {
              distLeft: "",
              distTop: "",
              height: "",
              id: "",
              page: 0,
              rectIndex: "",
              size: 0,
              sort: "",
              width: ""
            }
          ],
          imageId: "",
          page: 0,
          size: 0,
          sort: ""
        }
      ],
      // 每一页为对象然后 做一个简单的分页
      resetData:[] as any
    }
  },
  actions: {
    submitData() { },
    // 数据添加
    sortData(list) {
      const page = list.titleDesc
      // 1 暂无数据 2 有数据 3 数据位置
      if (this.hazardCites[0].page === 0) {
        console.log('一个数据都没得');
        this.hazardCites[0].imageId = list.imageId
        this.hazardCites[0].page = list.titleDesc
        this.hazardCites[0].distance[0] = {
          distLeft: list.distLeft,
          distTop: list.distTop,
          height: list.height,
          id: list.id,
          page: list.titleDesc,
          rectIndex: list.rectIndex,
          size: 0,
          sort: "",
          width: list.width
        }
      } else if (this.hazardCites[0].page !== 0) {
        // 判断位置 1 在前 2 在后  4 同一页 后面添加
        const arrSort = this.hazardCites.map(item => item.page)
        const arrIndex = arrSort.indexOf(page)
        const insertIndex = arrSort.findIndex((element) => {
          return element > page;
        });
        if (arrIndex !== -1) {
          // 同一页插入
          this.hazardCites[arrIndex].distance.push({
            distLeft: list.distLeft,
            distTop: list.distTop,
            height: list.height,
            id: list.id,
            page: list.titleDesc,
            rectIndex: list.rectIndex,
            size: 0,
            sort: "",
            width: list.width
          })
        } else if (insertIndex !== -1) {
          //开始 中间插入
          console.log("开始或者中间的第一个", insertIndex);
          this.hazardCites.splice(insertIndex, 0, {
            distance: [
              {
                distLeft: list.distLeft,
                distTop: list.distTop,
                height: list.height,
                id: list.id,
                page: list.titleDesc,
                rectIndex: list.rectIndex,
                size: 0,
                sort: "",
                width: list.width
              }
            ],
            imageId: list.imageId,
            page: list.titleDesc,
            size: 0,
            sort: ""
          })
        } else {
          // 最后插入
          this.hazardCites.push({
            distance: [
              {
                distLeft: list.distLeft,
                distTop: list.distTop,
                height: list.height,
                id: list.id,
                page: list.titleDesc,
                rectIndex: list.rectIndex,
                size: 0,
                sort: "",
                width: list.width
              }
            ],
            imageId: list.imageId,
            page: list.titleDesc,
            size: 0,
            sort: ""
          })
        }
      }
      console.log(this.hazardCites, 'listttt');
    },
    // 对添加的数据进行查询
    queryData(page){
      const arr = this.hazardCites.filter((item)=>{
        return item.page === page
      })
      return arr
    },
    clearData() {
      this.hazardCites = []
      this.hazardCites[0] = {
        distance: [
          {
            distLeft: "",
            distTop: "",
            height: "",
            id: "",
            page: 0,
            rectIndex: "",
            size: 0,
            sort: "",
            width: ""
          }
        ],
        imageId: "",
        page: 0,
        size: 0,
        sort: ""
      }
    },
    resetHistoryData(list){
      this.resetData=[]
      if(list.length>0){
        const pageNum =Array.from(new Set(list.map(item=>item.pageNumber))) 
        pageNum.map(item=>{
          this.resetData.push({
            pageNumber:item,
            filesId:list.filter(it=>it.pageNumber===item)[0].fileId,
            distance:list.filter(it=>it.pageNumber===item)
          })
        })
      }
    },
    clearHistoryData(){
      this.resetData=[]
    }
  },
})